//
//  LoginController.m
//  ChickenA
//
//  Created by 韦显光 on 2018/7/7.
//  Copyright © 2018年 xg韦. All rights reserved.
//

#import "LoginController.h"
#import "UIButton+countDown.h"
#import "RegistController.h"
#import <UMShare/UMShare.h>
#import "tabbarViewController.h"
#import "cPhonePSWController.h"
#import "JPUSHService.h"


@interface LoginController ()
{
    
}
@property (weak, nonatomic) IBOutlet UITextField *tsf1;
@property (weak, nonatomic) IBOutlet UITextField *tsf2;

@end

@implementation LoginController
- (IBAction)back:(UIButton *)sender {

   [self.navigationController popToRootViewControllerAnimated:YES];
}

- (IBAction)LoginBtna:(UIButton *)sender {
    if ( [FuncPublic TextFieldisError:_tsf1]|| [FuncPublic TextFieldisError:_tsf2]) {
             NSDictionary*dic;
             dic=@{@"phone":_tsf1.text,@"password":_tsf2.text};

        [FuncPublic getData:self parameters:dic RequestUrl:Userslogin success:^(NSDictionary *acountInfo) {
            
            //注册别名推送
            [JPUSHService setAlias:_tsf1.text completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                
            } seq:1];
            
                [FuncPublic SaveDefaultInfo:WXGStr(acountInfo[@"token"]) Key:@"token"];
//                [FuncPublic SaveDefaultInfo:WXGStr(acountInfo[@"result"][@"uid"]) Key:@"uid"];
            UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
            tabbarViewController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"tabbarView"];
            self.view.window.rootViewController=secondViewController;
            [self.view.window makeKeyAndVisible];
            
                [WSProgressHUD showImage:nil status:acountInfo[@"msg"]];

     }];
    }

}
- (IBAction)RegBtna:(UIButton*)sender {
    
    if ([[sender currentTitle]containsString:@"忘记密码"]) {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"bk" bundle:nil];
        cPhonePSWController* vc = [mainStoryBoard instantiateViewControllerWithIdentifier:@"cPhonePSW"];
        vc.type=@"forgit";
        [self presentViewController:vc animated:YES completion:^{
            
        }];
    }else
    {
        UIStoryboard *mainStoryBoard = [UIStoryboard storyboardWithName:@"login" bundle:nil];
        RegistController*secondViewController = [mainStoryBoard instantiateViewControllerWithIdentifier:@"Regist"];
        secondViewController.sms_type=[NSString stringWithFormat:@"%ld",sender.tag];
        [self presentViewController:secondViewController animated:YES completion:^{
            
        }];
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//第三方登陆
- (IBAction)OtherLonginBtna:(UIButton *)sender {
    [[UMSocialManager defaultManager] getUserInfoWithPlatform:sender.tag currentViewController:nil completion:^(id result, NSError *error) {
        UMSocialUserInfoResponse *resp = result;
        
        NSString*type=sender.tag==1?@"weixin":@"qq";
        NSDictionary*tempdic;
        if ([type isEqualToString:@"weixin"]) {
             tempdic=resp.originalResponse;
        }else if([type isEqualToString:@"qq"])
        {
            tempdic=@{@"name":resp.name,@"iconurl":resp.iconurl,@"gender":resp.gender,@"openid":resp.usid};
        }
       
        NSDictionary*dic2=@{@"logintype":type,@"openid":WXGStr(resp.usid)};
        NSMutableDictionary*passdic=dic2.mutableCopy;
        [passdic setDictionary:tempdic];
        [FuncPublic getData:self parameters:dic2 RequestUrl:sauthLogin success:^(NSDictionary *acountInfo) {
            if ([WXGStr(acountInfo[@"msg"])containsString:@"绑定手机号"]) {
                [MyUtils collviewSelidex:@"绑定手机号" toVc:self andShopInfo:passdic];
            }else
            {
                [WSProgressHUD showImage:nil status:@"登录成功"];
                [FuncPublic SaveDefaultInfo:@"1" Key:@"DENGLU"];
                [FuncPublic SaveDefaultInfo:acountInfo[@"token"] Key:@"token"];
                [self back:nil];
            }
            }];
    }];
    
}

@end
